<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>

    //http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html



    function show(time){

        var promise = new Promise(function(resolve,reject){
            setTimeout(function(){
                console.log(time+"秒后执行");
                resolve();
            },time*1000);
        });

        return promise;
    };

    function show2(time){

        var promise = new Promise(function(resolve,reject){
            setTimeout(function(){
                console.log(time+"秒后执行");
                reject();
            },time*2000);
        });

        return promise;
    };

    function show3(time){

        var promise = new Promise(function(resolve,reject){
            setTimeout(function(){
                console.log(time+"秒后执行");
                resolve();
            },time*3000);
        });

        return promise;
    };


    //all都执行成功
    var p = Promise.all([show(1),show2(2),show3(3)]);

    p.then(function(){
        console.log("都执行成功的时候才会执行这里...");
    },function(){
        console.log("只要有一个失败的时候就进入，并且阻止执行...");
    });

    //有一个执行成功
    var p = Promise.race([show(1),show2(1),show3(1)]);

    p.then(function(){
        console.log("只要有一个成功的时候才会执行这里...");
    },function(){
        console.log("只要有一个失败的时候就进入，并且阻止执行...");
    });



    function show(time){

        var promise = new Promise(function(resolve,reject){
            setTimeout(function(){
                console.log(time+"秒后执行");
                resolve();
            },time*1000);
        });

        return promise;
    };


    //catch的使用
    show(1).then(function(){
        console.log("操作1");
    }).catch(function(){
        console.log("操作1");
    });
</script>
</body>
</html>